<?php

class Category_deal_Controller extends Base_Controller {

    /**
     * 获取顶级目录(root除外)list
     * @return type json string
     */
    public function action_toplevel() {
        $categoryLogic = new CategoryLogic();
        $ret = $categoryLogic->getNextLevelCats(0);
        unset($categoryLogic);
        return Response::json($ret);
    }

    /**
     * 用户传递parent id, 返回该category id所属的自分类
     * @return type
     */
    public function action_nextlevel() {
        if (!Input::has('pid')) {
            return Response::json(array(
                        'errno' => UMSErrcode::PARA_ERR,
            ));
        }

        $pid = intval(Input::get('pid'));
        $categoryLogic = new CategoryLogic();
        $ret = $categoryLogic->getNextLevelCats($pid);
        unset($categoryLogic);
        return Response::json($ret);
    }

    public function action_add() {
        if (!Input::has('pid') || !Input::has('name')) {
            return Response::json(array(
                        'errno' => UMSErrcode::PARA_ERR,
            ));
        }
        $pid = intval(Input::get('pid'));
        $name = Input::get('name');
        $categoryLogic = new CategoryLogic();
        $ret = $categoryLogic->addCategory($pid, $name);
        unset($categoryLogic);
        if ($ret) {
            Response::json(
                    array(
                        'errno' => UMSErrcode::SUCCESS,
            ));
        } else {
            Response::json(
                    array(
                        'errno' => UMSErrcode::SYS_ERR,
            ));
        }
    }

    //TODO 还需更改video的category
    public function action_del() {
        if (!Input::has('id')) {
            return Response::json(array(
                        'errno' => UMSErrcode::PARA_ERR,
            ));
        }
        $id = intval(Input::get('id'));
        $categoryLogic = new CategoryLogic();
        $ret = $categoryLogic->deleteCategory($id);
        unset($categoryLogic);
        if ($ret) {
            Response::json(
                    array(
                        'errno' => UMSErrcode::SUCCESS,
            ));
        } else {
            Response::json(
                    array(
                        'errno' => UMSErrcode::SYS_ERR,
            ));
        }
    }

}

?>
